{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "A measurement is a central concept in quantum mechanics. An easy way to think about it as a sample from a probability distribution: it is a random variable with a number of outcomes, each outcome is produced with a certain probability. \n",
    "\n",
    "Measurement connect the quantum world to our classical one: we cannot directly observe the quantum state in nature, we can only gather statistics about it with measurements. It sounds like a harsh boundary between a quantum and a classical system that can only be bridged by measurement. The reality is more subtle: unless a quantum system is perfectly isolated, it interacts with its surrounding environment. This leads to introduction of mixed states, which in one limit recover classical probabilities.\n",
    "\n",
    "\n",
    "# More on the bra-ket notation\n",
    "\n",
    "Before we take a deep dive into what measurements are, we need to introduce one more notation to complement the ket: it called a bra and it is denoted by $\\langle\\psi|$ for some quantum state $|\\psi\\rangle$. Together they form the bra-ket or Dirac notation. A bra is the conjugate transpose of a ket, and the other way around. This also means that a bra is a row vector. For instance, this is the bra for $|0\\rangle$"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-19T19:48:29.966399Z",
     "start_time": "2018-11-19T19:48:29.899076Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "|0> ket:\n",
      " [[1]\n",
      " [0]]\n",
      "<0| bra:\n",
      " [[1 0]]\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "zero_ket = np.array([[1], [0]])\n",
    "print(\"|0> ket:\\n\", zero_ket)\n",
    "print(\"<0| bra:\\n\", zero_ket.T.conj())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This makes it very easy to write dot products: if we write a bra followed by a ket, that is exactly what the dot product is. This is so common that we often drop one of the vertical bars, and just write $\\langle 0|0\\rangle$, for instance. Since quantum states are normalized, the inner product of any quantum state with itself is always one:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-19T19:48:29.987334Z",
     "start_time": "2018-11-19T19:48:29.968450Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1]])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zero_ket.T.conj().dot(zero_ket)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Similarly, orthogonal vectors always give 0. E.g. $\\langle 0|1\\rangle$:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-19T19:48:30.000810Z",
     "start_time": "2018-11-19T19:48:29.989605Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0]])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "one_ket = np.array([[0], [1]])\n",
    "zero_ket.T.conj().dot(one_ket)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What about a ket and a bra? That is going to be a matrix: essentially the outer product of the two vectors. Here's $|0\\rangle\\langle 0|$:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-19T19:48:30.023616Z",
     "start_time": "2018-11-19T19:48:30.004334Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 0],\n",
       "       [0, 0]])"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "zero_ket.dot(zero_ket.T.conj())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This should look familiar: it is a projection to the first element of the canonical basis. It is true in general that $|\\psi\\rangle\\langle\\psi|$ is going to be a projector to $|\\psi\\rangle$. It is very intuitive: take some other quantum state $|\\phi\\rangle$ and apply the matrix $|\\psi\\rangle\\langle\\psi|$ on it: $|\\psi\\rangle\\langle\\psi|\\phi\\rangle$. Now the right-most two terms are a bra and a ket, so it is a dot product: the overlap between $|\\phi\\rangle$ and $|\\psi\\rangle$. Since this is a scalar, it just scales the left-most term, which is the ket $|\\psi\\rangle$, so in effect, we projected $|\\phi \\rangle$ on this vector."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Measurements\n",
    "\n",
    "A measurement in quantum mechanics is an operator-valued random variable. The theory of measurements is rich and countless questions about them are still waiting to be answered. Most quantum computers that we have today, however, only implement one very specific measurement, which makes our discussion a lot simpler. This measurement is in the canonical basis. In other words, the measurement contains two projections, $|0\\rangle\\langle 0|$ and $|1\\rangle\\langle 1|$, and this measurement can be applied to any of the qubits of the quantum computer.\n",
    "\n",
    "We already saw how applying a projection on a vector works. If we want to make a scalar value of that, we need to add a bra to the left. For instance, for some state $|\\psi\\rangle$, we get a scalar for $\\langle\\psi|0\\rangle\\langle 0|\\psi\\rangle$. This is called the expectation value of the operator $|0\\rangle\\langle 0|$. To put this in context, let us apply the projection $|0\\rangle\\langle 0|$ on the superposition $\\frac{1}{\\sqrt{2}}(|0\\rangle + |1\\rangle)$, which is the column vector $\\frac{1}{\\sqrt{2}}\\begin{bmatrix} 1\\\\ 1\\end{bmatrix}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-19T19:48:30.040800Z",
     "start_time": "2018-11-19T19:48:30.027650Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[0.5]])"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ψ = np.array([[1], [1]])/np.sqrt(2)\n",
    "Π_0 = zero_ket.dot(zero_ket.T.conj())\n",
    "ψ.T.conj().dot(Π_0.dot(ψ))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "That is exactly one half, the square of the absolute value of the probability amplitude corresponding to $|0\\rangle$ in the superposition! This is the mathematical formalism of what we had said earlier: given a state $|\\psi\\rangle = a_0|0\\rangle + a_1|1\\rangle$, we get an output $i$ with probability $|a_i|^2$. This is known as the *Born rule*. Now we have a recipe to extract probabilities with projections. This is exactly what is implemented in the quantum simulator. The measurement in the simulator is what we described here. Let's create an equal superposition with the Hadamard gate (see a later notebook for quantum circuits), apply the measurement, and observe the statistics:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-19T19:48:31.136144Z",
     "start_time": "2018-11-19T19:48:30.043322Z"
    }
   },
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "/home/pwittek/.anaconda3/envs/qiskit/lib/python3.7/site-packages/marshmallow/schema.py:364: ChangedInMarshmallow3Warning: strict=False is not recommended. In marshmallow 3.0, schemas will always be strict. See https://marshmallow.readthedocs.io/en/latest/upgrading.html#schemas-are-always-strict\n",
      "  ChangedInMarshmallow3Warning\n",
      "/home/pwittek/.anaconda3/envs/qiskit/lib/python3.7/site-packages/marshmallow/schema.py:364: ChangedInMarshmallow3Warning: strict=False is not recommended. In marshmallow 3.0, schemas will always be strict. See https://marshmallow.readthedocs.io/en/latest/upgrading.html#schemas-are-always-strict\n",
      "  ChangedInMarshmallow3Warning\n",
      "/home/pwittek/.anaconda3/envs/qiskit/lib/python3.7/site-packages/marshmallow/schema.py:364: ChangedInMarshmallow3Warning: strict=False is not recommended. In marshmallow 3.0, schemas will always be strict. See https://marshmallow.readthedocs.io/en/latest/upgrading.html#schemas-are-always-strict\n",
      "  ChangedInMarshmallow3Warning\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<Figure size 700x500 with 1 Axes>"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister\n",
    "from qiskit import execute\n",
    "from qiskit import BasicAer\n",
    "from qiskit.tools.visualization import plot_histogram\n",
    "\n",
    "backend = BasicAer.get_backend('qasm_simulator')\n",
    "q = QuantumRegister(1)\n",
    "c = ClassicalRegister(1)\n",
    "circuit = QuantumCircuit(q, c)\n",
    "circuit.h(q[0])\n",
    "circuit.measure(q, c)\n",
    "job = execute(circuit, backend, shots=100)\n",
    "plot_histogram(job.result().get_counts(circuit))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "You see that the outcome is random, with roughly half of the outcomes being 0.\n",
    "\n",
    "There is something additional happening. The measurement has a random outcome, but once it is performed, the quantum state is in the corresponding basis vector. That is, the superposition is destroyed. This is referred to as the collapse of the wavefunction. It is the subject of many ongoing debates and research results how and why it happens, but what matters to us is that we can easily calculate the quantum state after the measurement. Just projecting it to the basis vector is insufficient, since that would not be normalized, so we have to renormalize it. Mathematically it is expressed by the somewhat convoluted expression $\\frac{|i\\rangle\\langle i|\\psi\\rangle}{\\sqrt{\\langle\\psi|i\\rangle\\langle i|\\psi\\rangle}}$ if we observe the output $i$. For instance, if we observe zero after measuring the superposition $\\frac{1}{\\sqrt{2}}(|0\\rangle + |1\\rangle)$, the state after the measurement will be"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-19T19:48:31.147404Z",
     "start_time": "2018-11-19T19:48:31.139100Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1.],\n",
       "       [0.]])"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ψ = np.array([[np.sqrt(2)/2], [np.sqrt(2)/2]])\n",
    "Π_0 = zero_ket.dot(zero_ket.T.conj())\n",
    "probability_0 = ψ.T.conj().dot(Π_0.dot(ψ))\n",
    "Π_0.dot(ψ)/np.sqrt(probability_0)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "which is just a very long way of saying we get $|0\\rangle$.\n",
    "\n",
    "You can easily see this by putting two measurements in a sequence on the same qubit. The second one will always give the same outcome as the first. The first one is random, but the second one will be determined, since there will be no superposition in the computational basis after the first measurement. Let's simulate this by writing out the results of the two measurements into two different classical registers:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-19T19:48:31.191892Z",
     "start_time": "2018-11-19T19:48:31.149393Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'00': 39, '11': 61}"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "c = ClassicalRegister(2)\n",
    "circuit = QuantumCircuit(q, c)\n",
    "circuit.h(q[0])\n",
    "circuit.measure(q[0], c[0])\n",
    "circuit.measure(q[0], c[1])\n",
    "job = execute(circuit, backend, shots=100)\n",
    "job.result().get_counts(circuit)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "There is no output like 01 or 10."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Measuring multiqubit systems\n",
    "\n",
    "Most quantum computers implement local measurements, which means that each qubit is measured separately. So if we have a two qubit system where the first qubit is in the equal superposition and the second one is in $|0\\rangle$, that is, we have the state $\\frac{1}{\\sqrt{2}}(|00\\rangle + |01\\rangle)$, we will observe 0 and 0 as outcomes of the measurements on the two qubits, or 0 and 1. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-19T19:48:31.367295Z",
     "start_time": "2018-11-19T19:48:31.193956Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAdAAAAE+CAYAAAA9E0HyAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmcvOIA7rQAAIABJREFUeJzt3Xuc1nWd9/HXxxEQXEAGCQcJEEEaDtEkW2lKdqdWdnew2tRstbXWTbfczd1tt8Pe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gTYRwWmnncZdd90FwLe//W3OOeecfvg2Q0O/zgcqSSrmyCOP5DOf+QzvfOc72bdvHxdeeCG1tbUsWrSIuro63vjGN3LjjTeyePFijjzySMaNG8eXvvQlAH7wgx/wwAMPsHnzZr71rW8B8KUvfYl58+Zx1VVX8f73v59FixYxb9483vOe9wzk1zykOB+o84FKktpwPlBJkvqQASpJUgEGqCRJBRigkiQVYIBKklSAASpJUgEGqCRJBRigkiQVYIBKklSAAXqY626G+/3uuusuqqurqa+vB2DPnj1cfvnlvPrVr+aVr3wlX/jCFyruU5IOZQboYaynM9xv376dG2+8kZNPPvnAsrvuuovdu3dz//33c88993DTTTexdu3aHvcpSYc6A/Qw1tMZ7hctWsQVV1zBUUcddWBZRLBz50727t3LM888w/Dhwxk9enSP+5SkQ52zsRzGOprhfunSpc9r86tf/YonnniC17/+9Vx//fUHlr/lLW/hJz/5CbW1tezatYtrrrmGcePG9ahPaaB95BvjBroE9YHPXLylX3+fAXoY6242+meffZaPf/zjB6ZEamvp0qVUVVWxYsUKfvvb3/KmN72JM844wxnuJR02DNDDWHcz3P/ud7+jsbGRN7/5zQA89dRTXHjhhdx2221873vf43Wvex3Dhg1jwoQJvOIVr6C+vp7jjz++yz4laajwGuhhrLsZ7seMGcOqVatoaGigoaGBBQsWcNttt1FXV8fkyZO59957yUx27NjBww8/zEknndRtn5I0VHgEehjryQz3nXnf+97HBz/4QU499VQyk3e/+93MmTMHoMM+JWmoiY6uWR0u6urqcsmSJQNdhqR+5k1EQ1Nv3URUXV29NDMXdNfOU7iSJBVggEqSVIABKklSAQaoJEkFGKCSJBVggEqSVIABKklSAQaoJEkFGKCSJBVggEqSVIABKklSAQ4m3wscV3No6u/JeSUdWjwClSSpAANUkqQCDFBJkgowQCVJKsAAlSSpAANUkqQCDFBJkgowQCVJKqDfAzQiLo+IxyPimYhYGhGn93C70yJib0Qsb7f8vRGRHbyO6ptvIElSPwdoRJwHXAcsAuqAB4DFETGlm+3GATcD/6+TJjuBmravzHymt+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PvHrAChzADVM9T/pfqduC8859rAAABGklEQVScNvO0BjAFeBXwPmA68K7MfGTACpXU1f76YuCVwB8DU4EL3F97n3fhqr3ZwOOUHmEBStdDM3NNZt5O6Xrob4E/GKD6JD2ns/11bWbeQemRlu24v/YJA1TtNVM6TfuFiJi5/5GV/TJzN6XRiN44EMVJep7u9tdW3F/7jAGq5ymPo/lxSqMM3QxcFBEvjoijASJiFPAaYPnAVSkJ3F8HmtdA1aHy6EN/C7yF0iDyDwIbgTOBFuD9mbls4CqUtJ/768AwQNWl8i3ybwLeRmnYvuXAHZn56wEtTNILuL/2LwNUPea0ZdKhw/217xmgkiQV4E1EkiQVYIBKklSAASpJUgEGqCRJBRigkiQVYIBKklSAASpJUgH/H8gBLqyjsO3+AAAAAElFTkSuQmCC\n",
      "text/plain": [
       "<Figure size 504x360 with 1 Axes>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q = QuantumRegister(2)\n",
    "c = ClassicalRegister(2)\n",
    "circuit = QuantumCircuit(q, c)\n",
    "circuit.h(q[0])\n",
    "circuit.measure(q, c)\n",
    "job = execute(circuit, backend, shots=100)\n",
    "plot_histogram(job.result().get_counts(circuit))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "What happens if we make measurements on an entangled state? Let's look at the statistics again on the $|\\phi^+\\rangle$ state:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-19T19:48:31.507651Z",
     "start_time": "2018-11-19T19:48:31.369445Z"
    }
   },
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAAdAAAAE+CAYAAAA9E0HyAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAALEgAACxIB0t1+/AAAADl0RVh0U29mdHdhcmUAbWF0cGxvdGxpYiB2ZXJzaW9uIDMuMC4yLCBodHRwOi8vbWF0cGxvdGxpYi5vcmc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bOzHuALwBfo3Sd8jRKp2JXlpuMKL/aYjDwPWAJpYA/itJ11mfa2M9B6ZFHHuGkk05i6tSp3HDDDW/Zfuutt3LqqadyxhlncM455/DSSy8BUF9fz+c+9zlOPfVUTj/9dJ544om9+7zwwguceuqpTJ06la985Su05Tq7JHUXbRpEBBAR/YELgRMoBfDzwL9m5vb2L69jHeyDiHbv3s073/lO7rvvPqqqqjjrrLOYM2cOxx//x/FcW7ZsYeDAgQA8+OCDfP/73+fee+/llltuYf78+dx0002sX7+e8847j0cffZRDDjmEadOmMWvWLN75zndy3nnncckllzB9euMz91LXcRBRz9TZg4jafA00M7dn5r9k5ucz87LMvKU7hqdg3rx5HHvssYwaNYq+ffsyc+ZMHnzwwTe12ROeANu2bWPP4LGamhrOPPNMoDRcfNCgQcyfP59XXnmFrVu3ctJJJxERXHDBBTzwwANIUk+zz2ugETET+Glm7iq/b1Zm3tdulanD1dXVcdRRR+1drqqqYt68eW9pd8stt3DzzTdTX1/P/fffD8DEiRN54IEHmDlzJmvWrOGFF15gzZo1HHLIIVRVVb2pzz33BkpST9KaQUT3UrrG+bvy++YkpYkS1E00dfq+qckqLr74Yi6++GLuvfderr/+em6++WYuuugifvvb3/Le976XY445hpNOOonevXu3uk9J6u72GaCZeUhT79X9VVVVsWbNmr3La9eu5cgjG9+m+0czZ87kS1/6EgC9e/dm1qxZe7e9//3vZ/To0QwePHjvjDat6VOSuqs2BWJEnBERbwndiOhVno1I3cgJJ5xAbW0tK1eupL6+nvvuu4+zzz77TW2WL1++9/3DDz/McceVJpvatm3b3inYHnvsMXr37s3xxx/PkUceyRFHHMGzzz5LZnL33XczY8aMzvtQktRJ2nof6GOUZgj6XaP1g8vbPIXbjfTu3ZvrrruOc889l927d/PRj36U8ePHM2vWLKqrqznnnHOYM2cOv/zlL+nTpw+DBw/mpptuAuDVV1/l3HPPJSKoqqpi9uzZe/v95je/yec+9zl27NjBtGnTmDatyamOJalba+tcuG8AwzNzfaP17wCey8yBTe95YDrYb2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Kp4VuBuZTCs0zgO8AkzNzRReUKalVgqDfAAACZUlEQVQsIiqBC4DFmflQo23VwMXl7UOAYzJzTedX2bMYoGpRRAwGRgDvAj5M6doJlJ7VeghwR2Ze0zXVSWooIvoDmZk7Gg7sy/L/6CPi68CMzKzuqhp7Eu8D1VtExNuBvwS+ROlB6Nspnar9FfBNoA9wHPAQsLSLypTUSGZu3xOc2ejoKCIOAz4C3NoVtfVEHoHqLSLiNkqPj/spsJHS6dvJwDuA3wFfy0yvoUgHiPLI2q2NQ7NRm0MpDTL6QWbWd1pxPZgBqjcp/+t1K6XTPI83WDcCOBn4NDAaOC8zn++yQiXtFRHfpTT69hlgZWZuaaLN4Mz8facX14M5CleNTQBepnQLC1A6FZSZKzPzHuDPKZ3O/S9dVJ+kBiLiQuAzwPWUJj35RkR8OCKOK18T3XNt9PY9t6SpfXgEqjcpf9F+BhwGfAxY3niavvIEC5/OzD/tghIlNRARc4DdwHXATEqzhB0H1AAPAI8C44AbM7NvV9XZE3kEqjfJzO3Afwf6A3cAH4uIYyLicNg7EOFMYFHXVSkJICJ6Uzpj9PvMrM3Mb2bmZOCdwC8phekPgW8Dd3ZdpT2TR6BqUvlUz98CH6A0ifzTwHpgGlAHXJyZC7uuQkkAETEEGJ6ZL5VnCtvVcDBRRJwP/AA4ITNf6Ko6eyIDVC0q39LyZ8CHKE3btwj4UWa+1KWFSWpWebawyMzdEfEZSqdvD+vqunoaA1St5mPLpO4nIq4EemXmN7q6lp7GAJWkHiwi+gC7/cdv+zNAJUkqwFG4kiQVYIBKklSAASpJUgEGqCRJBRigkiQVYIBKklTA/wcV0BnbJYv5KwAAAABJRU5ErkJggg==\n",
      "text/plain": [
       "<Figure size 504x360 with 1 Axes>"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "q = QuantumRegister(2)\n",
    "c = ClassicalRegister(2)\n",
    "circuit = QuantumCircuit(q, c)\n",
    "circuit.h(q[0])\n",
    "circuit.cx(q[0], q[1])\n",
    "circuit.measure(q, c)\n",
    "job = execute(circuit, backend, shots=100)\n",
    "plot_histogram(job.result().get_counts(circuit))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We only observe 00 and 11. Since the state is $\\frac{1}{\\sqrt{2}}(|00\\rangle+|11\\rangle)$, this should not come as a shock. Yet, there is something remarkable going on here. At the end of the last section, we saw the same statistics, but from measurements on the same qubit. Now we have two, spatially separate qubits exhibiting the same behaviour: this is a very strong form of correlations. This means that if we measure just one qubit, and get, say, 0 as the outcome, we *know* with certainty that if we measured the other qubit, we would also get 0, even though the second measurement is also a random variable.\n",
    "\n",
    "To appreciate this better, imagine that your are tossing two unbiased coins. If you observe heads on one, there is absolutely nothing that you can say about what the other one might be other than a wild guess that holds with probability 0.5. If you play foul and you biased the coins, you might improve your guessing accuracy. Yet you can never say with certainty what the other coin will be based on the outcome you observed on one coin, except for the trivial case when the other coin deterministically gives the same face always.\n",
    "\n",
    "Remarkable as it is, there is no activation or instantaneous (faster than the speed of light) signalling happening between the qubits, though. Your measurement was local to the qubit and so is your information. If there is somebody else doing the measurement on the other qubit, you would have to inform the person through classical communication channels that you happen to know what the outcome will be. So while we certainly cannot violate the theory of relativity with entanglement, this strong form of correlation is still central to many quantum algorithms."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Mixed states\n",
    "\n",
    "If all was clear until now, this is where it gets messy. A ket and a bra is a projection, as we explained above. More than that, it is also a density matrix. A density matrix is another way of writing a quantum state, instead of kets. So, for instance we could write $\\rho = |\\psi\\rangle\\langle\\psi|$, where $\\rho$ is the density matrix for $|\\psi\\rangle$. The Born rule still applies, but now we have to take the trace of the result: for instance, $\\mathrm{Tr}[|0\\rangle\\langle 0|\\rho]$ would be the probability of seeing 0. See it in action:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-19T19:48:31.519211Z",
     "start_time": "2018-11-19T19:48:31.509701Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.4999999999999999"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "ψ = np.array([[1], [1]])/np.sqrt(2)\n",
    "ρ = ψ.dot(ψ.T.conj())\n",
    "Π_0 = zero_ket.dot(zero_ket.T.conj())\n",
    "np.trace(Π_0.dot(ρ))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "We get one half again. The renormalization after a measurement happens in a similar way: $\\frac{|0\\rangle\\langle 0|\\rho|0\\rangle\\langle 0|}{\\mathrm{Tr}[|0\\rangle\\langle 0|\\rho]}$."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-19T19:48:31.537553Z",
     "start_time": "2018-11-19T19:48:31.521246Z"
    }
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1., 0.],\n",
       "       [0., 0.]])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "probability_0 = np.trace(Π_0.dot(ρ))\n",
    "Π_0.dot(ρ).dot(Π_0)/probability_0"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "So why do we need this at all? Every state we have mentioned so far is called a *pure state*: these are kets or a density matrix created as a ket and a bra. There are other states called *mixed states*: these are *classical* probability distributions over pure states. Formally, a mixed state is written as $\\sum_i p_i |\\psi_i\\rangle\\langle\\psi_i|$, where $\\sum_i p_i=1$, $p_i\\geq 0$. This reflects our classical ignorance over the underlying quantum states. Compare the density matrix of the equal superposition $\\frac{1}{\\sqrt{2}}(|0\\rangle+|1\\rangle)$ and the mixed state $0.5(|0\\rangle\\langle 0|+|1\\rangle\\langle 1|)$:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2018-11-19T19:48:31.551225Z",
     "start_time": "2018-11-19T19:48:31.541006Z"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Density matrix of the equal superposition\n",
      "[[0.5 0.5]\n",
      " [0.5 0.5]]\n",
      "Density matrix of the equally mixed state of |0><0| and |1><1|\n",
      "[[0.5 0. ]\n",
      " [0.  0.5]]\n"
     ]
    }
   ],
   "source": [
    "zero_ket = np.array([[1], [0]])\n",
    "one_ket = np.array([[0], [1]])\n",
    "ψ = (zero_ket + one_ket)/np.sqrt(2)\n",
    "print(\"Density matrix of the equal superposition\")\n",
    "print(ψ.dot(ψ.T.conj()))\n",
    "print(\"Density matrix of the equally mixed state of |0><0| and |1><1|\")\n",
    "print((zero_ket.dot(zero_ket.T.conj())+one_ket.dot(one_ket.T.conj()))/2)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The off-diagonal elements are gone in the second case. The off-diagonal elements are also called coherences: their presence indicates that the state is quantum. The smaller these values are, the closer the quantum state is to a classical probability distribution.\n",
    "\n",
    "The second density matrix above has only diagonal elements and they are equal: this is the equivalent way of writing a uniform distribution. We know that the uniform distribution has maximum entropy, and for this reason, a density matrix with this structure is called a maximally mixed state. In other words, we are perfectly ignorant of which elements of the canonical basis constitute the state.\n",
    "\n",
    "We would like a quantum state to be perfectly isolated from the environment, but in reality, the quantum computers we have today and for the next couple of years cannot achieve a high degree of isolation. So coherences are slowly lost to the environment -- this is a process called decoherence. The speed at which this happens determines the length of the quantum algorithms we can run on the quantum computer: if it happens fast, we have time to apply a handful gates or do any other form calculation, and then we quickly have to pull out (measure) the results."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
